<?php
//The query class is included to make the queries.
include_once ("query.php");

/**
 * This class contains all the queries to handle groups in the system.
 */
class GroupHandler {

    private $model;
    private $params;

    /**
     *Main constructor of the class
     * @param <model> $model Model object
     */
    function __construct($model) {
        $this->model = $model;
        $this->params = $this->model->getParams();
    }

    /**
     *This function insert an user into the database
     * @param <string> $username Name of the user.
     * @param <boolean> $debug Variable to control the execution of the function
     * @return <integer> Returns the id of the user
     */
    function insertUser($username,$debug=false) {

        //If does not exists the element...
        if (0>$this->model->getId($username,"USUARIO",$debug))
        //inserting the new user...
            return $this->model->insertElement("USUARIO",$username,$debug);

        else {
            $mensaje[]="The user exists in the database.";
            $this->debug($mensaje, $debug);
        }
    }
    /**
     This function removes a tutor from the database
     @param <string> $username Name of the user
     @param <bool> $debug Var to debug the function. True to debug, false otherwise.
     @return <string> Returns NULL if the function fails, otherwise it returns the id ot the tutor inserted
     **/
    function removeTutor($username, $debug=false) {
        //If doesn't exists the username...
        if (0>$this->model->getId($username,"TUTOR",$debug)) {
            $mensaje[]="The user doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
//First we have to retrieve all the groups of this tutor
        $groups = $this->getGroupsFromTutor($username, $debug);
        if (count($groups)>0) {
            //for each group the tutor monitorizes,
            foreach ($groups as $group) {
                $coursename = $this->getCourseFromGroup($group,$debug);
                $this->removeTutorInGroup($username,$group,$coursename,$debug);
            }
            //Finally delete the tutor
            $this->model->deleteElement($username,"TUTOR",$debug);
        }
    }

    /**
     *This function insert an tutor into the database
     * @param <string> $username Name of the tutor.
     * @param <boolean> $debug Variable to control the execution of the function
     * @return <integer> Returns the id of the user
     */
    function insertTutor($username,$debug=false) {
        //If does not exists the element...
        if (0>$this->model->getId($username,"TUTOR",$debug))
        //inserting the new user
            return $this->model->insertElement("TUTOR",$username,$debug);

        else {
            $mensaje[]="The tutor exists in the database.";
            $this->debug($mensaje, $debug);
        }
    }


    /**
     *This function insert a group into the database
     * @param <string> $groupname name of the group
     * @param <string> $coursename name of the course of the group
     * @param <boolean> $debug boolean to control the debugging mode
     */
    function createGroup($groupname,$coursename,$debug=false) {

        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="createGroup: The course doesn't exists.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //Inserting the courseName first...
        //$courseID=$this->model->insertElement("CURSO",$coursename,$debug);
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug))
        //Inserting the groupName first...
            $groupID=$this->model->insertElement("GRUPO",$groupname,$debug);
        //else get the existent id

        $groupID=$this->model->getId($groupname,"GRUPO",$debug);


        //If doesnt exists the relation...
        if (!$this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug))

        //Insert the relation that handle the group with the course
            $this->model->createRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug);



        //If doesn't exists the relation...
//        if (!$this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug))

        //Insert the relation that handle the group with the course
//            $this->model->createRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug);
        else {
            $mensaje[]="The relation already exists on the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
    }

    /**
     *This function removes all the users in a group and after that it removes
     * the group from the database.
     * @param <type> $groupname Name of the group to remove
     * @param <type> $coursename Course of the group
     * @param <type> $debug Var to debug
     * @return <type> True if the group has been correctly removed. False otherwise.
     */
    function removeGroup($groupname,$coursename,$debug=false) {

        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="Can't remove the group, the course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return false;
        }
        //get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="Can't remove the group, the group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return false;
        }
        //else get the existent id
        else $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //else get the existent id

        //Remove all users from the group
        $this->removeAllUsersInGroup($groupname,$coursename,$debug);
        $mensaje[]="Users removed.";
        //remove all the tutor in a group
        $arrayTutores=$this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."TUTOR-?",$debug);

        foreach ($arrayTutores as $element) {
            $this->model->deleteRelation($element,$debug);
        }
        $mensaje[]="Tutors removed.";
        $this->model->deleteRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID);
        //Delete the group
        $this->model->deleteElement($groupname,"GRUPO",$debug);
        $mensaje[]="Group removed.";
        $this->debug($mensaje, $debug);
        return true;
    }

    /**
     *Function to debug
     * @param <type> $mensaje Array with strings to show
     * @param <bool> $debug Var to debug or not.
     */
    function debug($mensaje, $debug) {
        if(($debug)&& (count($mensaje)>0)) {
            foreach($mensaje as $mens) {
                trigger_error($mens,E_USER_NOTICE);
            }
        }
    }

    /**
     *This function get all the groupnames grom a course
     * @param <string> $coursename String with the name of the course
     * @param <boolean> $debug var to debug
     * @return <array> Array with the groups from a course.
     */
    function getAllGroupsFromCourse($coursename,$debug=false) {
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $courseID=$this->model->getId($coursename,"CURSO",$debug);
            $arrayDatos = array();
            $arrayDatos= $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO-?");
            $arrayFinal = array();
            foreach ($arrayDatos as $element) {
                $stringExplode = explode($this->params['separator'],$element);
                $stringExplode2 = explode($this->params['id_char'],$stringExplode[1]);
                $arrayFinal[]= $stringExplode2[1];
            }
            $newarray =$this->model->getElementNameFromIds($arrayFinal);
            $this->debug($mensaje, $debug);
            return $newarray;
        }
    }

    /**
     *This function get all the users from a group name
     * @param <string> $groupname
     * @return <array> array with the usersids
     * @param <boolean> $debug boolean to control the debugging mode
     */
    function getUsersfromGroup($groupname,$coursename,$debug=false) {
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //If doesn't exists the group...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);
            $courseID=$this->model->getId($coursename,"CURSO",$debug);
            $arrayDatos = array();
            $arrayDatos= $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO-?");
            $arrayFinal = array();
            foreach ($arrayDatos as $element) {
                $stringExplode = explode($this->params['separator'],$element);
                $stringExplode2 = explode($this->params['id_char'],$stringExplode[2]);
                $arrayFinal[]= $stringExplode2[1];
            }
            $newarray =$this->model->getElementNameFromIds($arrayFinal);
            return $newarray;
        }
    }

    /**
     *This function returns the id of the relation CURSO-GRUPO
     * @param <string> $groupname Name of the group
     * @param <bool> $debug Var to debug
     * @return <string> id of the course
     */
    function getCourseIdFromGroup($groupname,$coursename,$debug=false) {
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //If doesn't exists the group...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);
            $courseID=$this->model->getId($coursename,"CURSO",$debug);
            $arrayDatos = array();
            $arrayDatos= $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID);
            $string = $arrayDatos[0];
            $stringExplode = explode($this->params['separator'],$string);
            $stringExplode2 = explode($this->params['id_char'],$stringExplode[0]);
            return $stringExlode2[1];
        }
    }

    /**
     *Get the group id from a username
     * @param <string> $username String with the username
     * @param <string> $coursename String with the coursename
     * @param <bool> $debug Var to debug
     * @return <string> id of the group
     */
    function getGroupFromUser($username,$coursename,$debug=false) {
        //If doesn't exists the username...
        if (0>$this->model->getId($username,"USUARIO",$debug)) {
            $mensaje[]="The user doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $userID=$this->model->getId($username,"USUARIO",$debug);

        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);
        $arrayDatos = array();
        $arrayDatos= $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO-?".$this->params['separator']."USUARIO".$this->params['id_char'].$userID);
        $string = $arrayDatos[0];
        $stringExplode = explode($this->params['separator'],$string);
        $stringExplode2 = explode($this->params['id_char'],$stringExplode[1]);
        if ($stringExplode2[1]==NULL) {
            $mensaje[]="getGroupFromUser: the group result is NULL.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            return $stringExplode2[1];
        }
    }


    /**
     *This function insert a user in a specific group with a coursename given. If the user
     * doesn't exists, it creates the user firstly.
     * @param <string> $username Username to insert in the group
     * @param <string> $groupname Group name to be inserted.
     * @param <string> $coursename Coursename of the group
     * @param <bool> $debug Var to debug the function
     * @return <type> NULL if inserted incorrectly.
     */
    function insertUserInGroup($username,$groupname,$coursename,$debug=false) {

        //If doesn't exists the username...
        if (0>$this->model->getId($username,"USUARIO",$debug)) {
            $mensaje[]="The username doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $userID=$this->model->getId($username,"USUARIO",$debug);

        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);
        //If doesn't exists the relation...
        if (!$this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO".$this->params['id_char'].$userID,$debug))
        //Insert the relation that handle the group with the course
            $this->model->createRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO".$this->params['id_char'].$userID,$debug);
    }

    /**
     *This function sets a tutor in a given group
     * @param <string> $username String with the tutorname
     * @param <string> $groupname String with the groupname
     * @param <string> $coursename String with the coursename
     * @param <bool> $debug Var to debug
     * @return <type> It returns NULL if the function fails.
     */
    function setTutorInGroup($username,$groupname,$coursename,$debug=false) {

        //If doesn't exists the tutor...
        if (0>$this->model->getId($username,"TUTOR",$debug)) {
            $mensaje[]="The tutor doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else $userID=$this->model->getId($username,"TUTOR",$debug);

        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        //If doesn't exists the relation CURSO-GRUPO...
        if (!$this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug)) {
            $mensaje[]="setTutorInGroup: The course-group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $this->model->createRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."TUTOR".$this->params['id_char'].$userID,$debug);
            $mensaje[]="setTutorInGroup: The the tutor has been inserted.";


        }
        //Insert the relation that handle the group with the course
        $this->debug($mensaje, $debug);

    }

    /**
     *This function get all the groups from a given tutor. If coursename is not
     * empty, the function will return only the groups of this course, otherwise
     * it returns all the groups of all courses.
     * @param <string> $username string with the tutorname
     * @param <bool> $debug var to debug
     * @param <string> $coursename string with the coursename
     * @return <array> array with the names of the groups
     */
    function getGroupsFromTutor($username, $debug,$coursename="") {
        //If doesn't exists the username...
        if (0>$this->model->getId($username,"TUTOR",$debug)) {
            $mensaje[]="The username doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else $userID = $this->model->getId($username,"TUTOR",$debug);

        if (!$coursename == "") {
            if (0>$this->model->getId($coursename,"CURSO",$debug)) {
                $mensaje[]="The course doesn't exists in the database.";
                $this->debug($mensaje, $debug);
                return NULL;
            }else $courseID = $this->model->getId($coursename,"CURSO",$debug);
        } else $courseID = "?";
        $arrayDatos=$this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO-?".$this->params['separator']."TUTOR".$this->params['id_char'].$userID,$debug);
        $arrayFinal = array();
        foreach ($arrayDatos as $element) {
            $stringExplode = explode($this->params['separator'],$element);
            $stringExplode2 = explode($this->params['id_char'],$stringExplode[1]);
            $arrayFinal[]= $stringExplode2[1];
        }
        $newarray =$this->model->getElementNameFromIds($arrayFinal);
        return $newarray;
    }


    public function deleteActividad($actName,$debug) {
        //If doesn't exists the activity...
        if (0>$this->model->getId($actName,"ACTIVIDAD",$debug)) {
            $mensaje[]="The activity doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $actID = $this->model->getId($actName,"ACTIVIDAD",$debug);
            $relaciones = $this->model->getRelation("USUARIO-?".$this->params['separator']."ACTIVIDAD-".$actID,$debug);
            $estructuras =$this->model->getAllNodes("/SISTEMA/ACTIVIDAD",true);
            $estructuras2 = $this->model->getAllNodes("/SISTEMA/USUARIO-ACTIVIDAD",true);
            //Deleting relations and values of /SISTEMA/USUARIO-ACTIVIDAD
            foreach ($relaciones as $relacion) {
                foreach ($estructuras2 as $esc2) {
                    $this->model->deleteValue("/".$relacion.$esc2,$debug);
                }
                $this->model->deleteRelation($relacion);
            }
            foreach ($estructuras as $esc) {
                $this->model->deleteValue("/ACTIVIDAD-".$actID.$esc,$debug);
            }
            $this->model->deleteElement($actName,"ACTIVIDAD",$debug);

        }
    }
    /**
     *This function returns all the tutors contained in a given course.
     * @param <string> $coursename String with the coursename
     * @param <bool> $debug var to debug
     * @return <array> Array with the tutors
     */
    function getTutorsFromCourse($coursename,$debug=false) {
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            //get all the coursenames of this course
            $array  = $this->getAllGroupsFromCourse($coursename, $debug);
            $arrayTutors = array();
            foreach ($array as $groupname) {
                $array2 = $this->getTutorsInGroup($groupname, $coursename, $debug);
                foreach ($array2 as $tutor) {
                    ////Avoiding empty values and repetitions.
                    if (($tutor!=NULL)&& (!in_array($tutor,$arrayTutors))) {
                        $arrayTutors[]= $tutor;
                    }
                }
            }
            return $arrayTutors;
        }
    }



    /**
     *This function gets the current tutor of an existing group
     * @param <type> $groupname Name of the group
     * @param <type> $coursename Name of the course
     * @param <type> $debug Var to debug
     * @return <type> Name of the tutor
     */
    function getTutorsInGroup($groupname,$coursename,$debug=false) {
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);
        //If doesn't exists the relation...
        if (!$this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID,$debug)) {
            $mensaje[]="setTutorInGroup: The course-group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else {
            $arrayDatos =  $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."TUTOR-?",$debug);
            $arrayFinal = array();
            foreach ($arrayDatos as $element) {
                $stringExplode = explode($this->params['separator'],$element);
                $stringExplode2 = explode($this->params['id_char'],$stringExplode[2]);
                $arrayFinal[]= $stringExplode2[1];
            }
            $newarray =$this->model->getElementNameFromIds($arrayFinal);
            return $newarray;
        }
    }

    /**
     *This function remove a user from a given group
     * @param <string> $username Username to be removed
     * @param <string> $groupname Groupname to use
     * @param <string> $coursename Coursename of the group
     * @param <type> $debug var to debug
     * @return <string> NULL if the function fails.
     */
    function removeUserInGroup($username,$groupname,$coursename,$debug=false) {
        //If doesn't exists the username...
        if (0>$this->model->getId($username,"USUARIO",$debug)) {
            $mensaje[]="The username doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $userID=$this->model->getId($username,"USUARIO",$debug);
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        //If exists the relation...
        if ($this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO".$this->params['id_char'].$userID,$debug))
        //delete it
            $this->model->deleteRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO".$this->params['id_char'].$userID,$debug);
        else {
            $mensaje[]="The relation doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
    }

    /**
     *This function returns a course from a given group.
     * @param <string> $groupname String with the name of the group
     * @param <bool> $debug Var to debug the function
     * @return <string> String with the course.
     */
    function getCourseFromGroup($groupname,$debug=false) {
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $groupID=$this->model->getId($coursename,"GRUPO",$debug);
        $arrayDatos = array();
        $arrayDatos= $this->model->getRelation("CURSO-?".$this->params['separator']."GRUPO".$this->params['id_char'].$groupID);
        $element = $arrayDatos[0];
        $stringExplode = explode($this->params['separator'],$element);
        $stringExplode2 = explode($this->params['id_char'],$stringExplode[0]);
        return $this->model->getNameElementId($stringExplode2[1]);
    }

    /**
     *This function remove a tutor from a given group
     * @param <string> $username tutorname to be removed
     * @param <string> $groupname Groupname to use
     * @param <string> $coursename Coursename of the group
     * @param <type> $debug var to debug
     * @return <string> NULL if the function fails.
     */
    function removeTutorInGroup($tutorname,$groupname,$coursename,$debug=false) {
        //If doesn't exists the tutorname...
        if (0>$this->model->getId($tutorname,"TUTOR",$debug)) {
            $mensaje[]="The tutorname doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $userID=$this->model->getId($tutorname,"TUTOR",$debug);
        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);
        //If exists the relation...
        if ($this->model->existsRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."TUTOR".$this->params['id_char'].$userID,$debug))
        //delete it
            $this->model->deleteRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."TUTOR".$this->params['id_char'].$userID,$debug);
        else {
            $mensaje[]="The relation doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
    }

    /**
     *This function change a user from a old group to a new group. Both groups must exists on the database.
     * @param <string> $username Username to be removed
     * @param <string> $oldgroupname Old groupname to use
     * @param <string> $newgroupname New groupname to use
     * @param <string> $coursename Coursename of the group
     * @param <type> $debug var to debug
     * @return <string> NULL if the function fails.
     */
    function changeUserInGroup($username,$oldgroupname,$newgroupname,$coursename,$debug=false) {
        //If doesn't exists the username...
        if (0>$this->model->getId($username,"USUARIO",$debug)) {
            $mensaje[]="The username doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $userID=$this->model->getId($username,"USUARIO",$debug);

        //If doesn't exists the old group...
        if (0>$this->model->getId($oldgroupname,"GRUPO",$debug)) {
            $mensaje[]="The oldgroup doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($oldgroupname,"GRUPO",$debug);

        //If doesn't exists the new group...
        if (0>$this->model->getId($newgroupname,"GRUPO",$debug)) {
            $mensaje[]="The newgroup doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($newgroupname,"GRUPO",$debug);
        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        $this->removeUserInGroup($username,$oldgroupname,$coursename,$debug);

        $this->insertUserInGroup($username,$newgroupname,$coursename,$debug);
    }



    /**
     *This function change a tutor from a old group to a new group. Both groups must exists on the database.
     * @param <string> $username tutorname to be removed
     * @param <string> $oldgroupname Old groupname to use
     * @param <string> $newgroupname New groupname to use
     * @param <string> $coursename Coursename of the group
     * @param <type> $debug var to debug
     * @return <string> NULL if the function fails.
     */
    function changeTutorInGroup($username,$oldgroupname,$newgroupname,$coursename,$debug=false) {

        //If doesn't exists the tutorname...
        if (0>$this->model->getId($username,"TUTOR",$debug)) {
            $mensaje[]="The tutorname doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }

        //If doesn't exists the old group...
        if (0>$this->model->getId($oldgroupname,"GRUPO",$debug)) {
            $mensaje[]="The oldgroup doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }

        //If doesn't exists the new group...
        if (0>$this->model->getId($newgroupname,"GRUPO",$debug)) {
            $mensaje[]="The newgroup doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }

        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }

        $this->removeTutorInGroup($username,$oldgroupname,$coursename,$debug);

        $this->setTutorInGroup($username,$newgroupname,$coursename,$debug);
    }

    /**
     *This function will remove all the users grom a given group
     * @param <string> $groupname Groupname to use
     * @param <string> $coursename Coursename of the group
     * @param <type> $debug var to debug
     * @return <string> NULL if the function fails.
     */
    function removeAllUsersInGroup($groupname,$coursename,$debug=false) {

        //If doesn't exists the group...
        if (0>$this->model->getId($groupname,"GRUPO",$debug)) {
            $mensaje[]="The group doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;
        }
        else
            $groupID=$this->model->getId($groupname,"GRUPO",$debug);

        //If doesn't exists the course...
        if (0>$this->model->getId($coursename,"CURSO",$debug)) {
            $mensaje[]="The course doesn't exists in the database.";
            $this->debug($mensaje, $debug);
            return NULL;

        }
        //else get the existent id
        else $courseID=$this->model->getId($coursename,"CURSO",$debug);

        $arrayDatos = array();
        $arrayDatos= $this->model->getRelation("CURSO".$this->params['id_char'].$courseID.$this->params['separator']."GRUPO".$this->params['id_char'].$groupID.$this->params['separator']."USUARIO-?");

        foreach ($arrayDatos as $element) {

            $this->model->deleteRelation($element);
        }
    }
}

?>
